var EVENT_MAP = {
  "k_8": KeyEvent.BACK_KEY, //返回
  "k_13": KeyEvent.OK_KEY, //ok
  "k_38": KeyEvent.UP_KEY, //上
  "k_37": KeyEvent.LEFT_KEY, //左
  "k_39": KeyEvent.RIGHT_KEY, //右
  "k_40": KeyEvent.DOWN_KEY, //下
  "k_33": KeyEvent.PAGEUP_KEY, //上一页
  "k_34": KeyEvent.PAGEDOWN_KEY, //下一页

  "k_48": KeyEvent.ZERO_KEY, //0
  "k_49": KeyEvent.ONE_KEY, //1
  "k_50": KeyEvent.TWO_KEY, //2
  "k_51": KeyEvent.THREE_KEY, //3
  "k_52": KeyEvent.FOUR_KEY, //4
  "k_53": KeyEvent.FIVE_KEY, //5
  "k_54": KeyEvent.SIX_KEY, //6
  "k_55": KeyEvent.SEVEN_KEY, //7
  "k_56": KeyEvent.EIGHT_KEY, //8
  "k_57": KeyEvent.NINE_KEY, //9
  "k_105": KeyEvent.POUND_KEY, //#,输入法切换
  "k_106": KeyEvent.STAR_KEY, //*

  "k_264": KeyEvent.FFWD_KEY, //快进
  "k_265": KeyEvent.FBWD_KEY, //快退
  "k_257": KeyEvent.CHANNELUP_KEY, //频道加
  "k_258": KeyEvent.CHANNELDOWN_KEY, //频道减
  "k_259": KeyEvent.VOLUMEUP_KEY, //音量加
  "k_260": KeyEvent.VOLUMEDOWN_KEY, //音量减
  "k_261": KeyEvent.SILENT_KEY, //静音
  "k_262": KeyEvent.AUDIOTRACK_KEY, //音轨
  "k_263": KeyEvent.PAUSEPLAY_KEY, //>||，播放，暂停
  "k_270": KeyEvent.STOP_KEY, //停止
  "k_272": KeyEvent.PORTAL_KEY, //首页
  "k_286": KeyEvent.AUDIOCHANNEL_KEY, //声道
  "k_560": KeyEvent.SUBTITLE_KEY, //字幕

  "k_275": KeyEvent.RED_KEY, //快捷键（红色）
  "k_276": KeyEvent.GREEN_KEY, //快捷键（绿色）
  "k_277": KeyEvent.YELLOW_KEY, //快捷键（黄色）
  "k_278": KeyEvent.BLUE_KEY, //快捷键（蓝色）
  "k_271": KeyEvent.POSITION_KEY, //定位
  "k_42": KeyEvent.POSITION_KEY, //定位 兼容
  "k_281": KeyEvent.FAVORITE_KEY, //收藏夹
  //	"k_284": KeyEvent.HELP_KEY,  //帮助
  "k_515": KeyEvent.HELP_KEY, //帮助
  "k_768": STBEvent.KEY_IPTV_EVENT, //虚拟事件

  "k_268": KeyEvent.INFO_KEY, //信息
  "k_1105": KeyEvent.SEARCH_KEY, //搜索

  "k_1108": KeyEvent.BTV_KEY, //直播
  "k_1109": KeyEvent.VOD_KEY, //点播
  "k_1110": KeyEvent.TVOD_KEY, //回看
  "k_1111": KeyEvent.COMM_KEY, //通信
  "k_1112": KeyEvent.NVOD_KEY, //轮播
  "k_283": KeyEvent.UNDERLINE_KEY, //下划线
  "k_112": KeyEvent.F1_KEY, //F1
  "k_113": KeyEvent.F2_KEY, //F2
  "k_114": KeyEvent.F3_KEY, //F3
  "k_115": KeyEvent.F4_KEY, //F4
  //"k_282": KeyEvent.BOOKMARK_KEY,//书签
  "k_1377": KeyEvent.VOICE_KEY,

  "k_280": KeyEvent.KEYLAST_KEY, //快速切换
  "k_1120": KeyEvent.OPTION_KEY, //OPTION键
  "k_132": KeyEvent.PIP_KEY, //画中画
  "k_1378": KeyEvent.MULTIVIEW_KEY //四屏同看
};

// getSTBEvent = function (event) {
//   eventJson = eval(Utility.getEvent());
//   var typeStr = eventJson.type;
//   switch (typeStr) {
//     case "EVENT_MEDIA_RECONNECT":
//       return STBEvent.EVENT_MEDIA_RECONNECT;
//     case "PREVIEW_TIME_END":
//       return STBEvent.PREVIEW_TIME_END;
//     case "EVENT_CHANNEL_LIST_TAMPERED":
//       return STBEvent.EVENT_CHANNEL_LIST_TAMPERED;
//     case "EVENT_ERROR_CA":
//       return STBEvent.EVENT_ERROR_CA;
//     case "EVENT_MEDIA_ERROR":
//       STBEvent.EVENT_MEDIA_ERROR.parameter = parseInt(eventJson.error_code, 10);
//       return STBEvent.EVENT_MEDIA_ERROR;
//     case "EVENT_MEDIA_END":
//       STBEvent.EVENT_MEDIA_END.parameter = eventJson;
//       return STBEvent.EVENT_MEDIA_END;
//     case "EVENT_MEDIA_BEGINING":
//       STBEvent.EVENT_MEDIA_BEGINING.parameter = eventJson;
//       return STBEvent.EVENT_MEDIA_BEGINING;
//     case "EVENT_PLAYMODE_CHANGE":
//       STBEvent.EVENT_PLAYMODE_CHANGE.parameter = eventJson;
//       return STBEvent.EVENT_PLAYMODE_CHANGE;
//     case "EVENT_PLTVMODE_CHANGE":
//       STBEvent.EVENT_PLTVMODE_CHANGE.parameter = parseInt(eventJson.service_type, 10);
//       return STBEvent.EVENT_PLTVMODE_CHANGE;
//     case "EVENT_GO_CHANNEL":
//       return STBEvent.EVENT_GO_CHANNEL;
//     case "EVENT_PPV_START":
//       STBEvent.EVENT_PPV_START.parameter = eventJson.message;
//       return STBEvent.EVENT_PPV_START;
//     case "EVENT_PPV_REMINDER_END":
//       STBEvent.PPV_REMINDER_END.parameter = eventJson.message;
//       return STBEvent.PPV_REMINDER_END;
//     case "EVENT_UNIVERSAL_REMINDER":
//       STBEvent.EVENT_UNIVERSAL_REMINDER.parameter = eventJson.message;
//       return STBEvent.EVENT_UNIVERSAL_REMINDER;
//     case "EVENT_NEW_VERSION":
//       return STBEvent.EVENT_NEW_VERSION;
//     case "EVENT_STB_HEARTBEAT":
//       return STBEvent.EVENT_STB_HEARTBEAT;
//     case "EVENT_STB_ERROR":
//       STBEvent.EVENT_STB_ERROR.parameter = eventJson;
//       return STBEvent.EVENT_STB_ERROR;
//     case "EVENT_STB_RESTORE":
//       STBEvent.EVENT_STB_RESTORE.parameter = eventJson;
//       return STBEvent.EVENT_STB_RESTORE;
//     case "EVENT_TVMS":
//       STBEvent.EVENT_TVMS.parameter = eventJson;
//       return STBEvent.EVENT_TVMS;
//     case "EVENT_POWEROFF_WAKEUP":
//       return STBEvent.EVENT_POWEROFF_WAKEUP;
//     case "EVENT_CONTROL_SYNCTOSCREEN":
//       STBEvent.EVENT_CONTROL_SYNCTOSCREEN.parameter = eventJson;
//       return STBEvent.EVENT_CONTROL_SYNCTOSCREEN;
//     default:
//       return null;
//   }
// }

document.onirkeydown = grabEvent;

document.onkeydown = grabEvent;



function grabEvent(event) {
  var keycode = event.which | event.keyCode;
  var resultEvent = EVENT_MAP["k_" + keycode];
  switch (resultEvent) {
    case KeyEvent.BACK_KEY:
      keyBack && keyBack();
      break;
    case KeyEvent.OK_KEY:
      keyEnter && keyEnter();
      break;
    case KeyEvent.UP_KEY:
      keyUp && keyUp();
      break;
    case KeyEvent.LEFT_KEY:
      keyLeft && keyLeft();
      break;
    case KeyEvent.RIGHT_KEY:
      keyRight && keyRight();
      break;
    case KeyEvent.DOWN_KEY:
      keyDown && keyDown();
      break;
    case KeyEvent.VOLUMEUP_KEY:
      volumeUp && volumeUp();
      break;
    case KeyEvent.VOLUMEDOWN_KEY:
      volumeDown && volumeDown();
      break;
    case KeyEvent.SILENT_KEY:
      keyMute && keyMute();
      break;
    case STBEvent.KEY_IPTV_EVENT:
      eval("eventJson=" + Utility.getEvent());
      if (eventJson.type == 'EVENT_MEDIA_END') {
        goUtility && goUtility();
      } else if ('EVENT_PLAYMODE_CHANGE' == eventJson.type) {
        fn && fn();
      }
      break;
  }
}